using System;
using UnityEngine;


namespace Bullet
{
    public class Bullet : MonoBehaviour
    {
        private float moveSpeed = 10f;
        public Vector3 Target = Vector3.zero;
        public Vector3 dir = Vector3.zero;
        private GameObject _effect;//当前子弹的击中特效

        private void Start()
        {
            //从玩家正前方发射子弹
            dir = Player.Player.instance.point.transform.forward;
            transform.position = Player.Player.instance.point.transform.position;
            _effect = Resources.Load<GameObject>("Prefabs/Effect/BulletHitEffect");
        }

        //因为子弹会重新激活，所以需要OnEnable
        private void OnEnable()
        {
            //从玩家正前方发射子弹
            dir = Player.Player.instance.point.transform.forward;
            transform.position = Player.Player.instance.point.transform.position;
        }

        void Update()
        {
            if(Target != Vector3.zero)//因为把环境的y轴提高了10，这样不会存在敌人是0 0 0的情况
                MoveToEnemy();
        }
        //向敌人的位置移动
        public void MoveToEnemy()
        {       
            //将子弹转向dir
            transform.rotation = Quaternion.LookRotation(dir);
            //向dir方向移动
            transform.Translate(dir * moveSpeed * Time.deltaTime, Space.World);
        }

        private void OnTriggerEnter(Collider other)
        {
            if(other.gameObject.layer == LayerMask.NameToLayer("Enemy"))
            {
                //播放音效
                //生成粒子特效
                PoolManager.Release(_effect, transform.position);
                gameObject.SetActive(false);
            }
        }
    }
}
